package com.chookss.home.audit.clockIn;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chookss.R;
import com.chookss.home.gallery.CusTomDrawerPopupListener;
import com.chookss.tools.Utils;
import com.chookss.view.DrawableTextView;
import com.lxj.xpopup.core.DrawerPopupView;

/**
 * Description: 自定义抽屉弹窗
 * Create by dance, at 2018/12/20
 */
public class CustomClockInRecordPopupView extends DrawerPopupView {
    TextView tvReset, tvSure;
    DrawableTextView tvTime;
    LinearLayout llType2, llType1,llType3;
    TextView tvType0, tvType1, tvType2, tvType3, tvType4, tvType5,tvType6,tvType7,tvType8;
    LinearLayout llBottom;
    private CusTomDrawerPopupListener listener = null;

    public CustomClockInRecordPopupView(@NonNull Context context) {
        super(context);
    }

    public CustomClockInRecordPopupView(@io.reactivex.annotations.NonNull Context context, CusTomDrawerPopupListener listener) {
        super(context);
        this.listener = listener;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.custom_clock_in_record_popup;
    }

    @Override
    protected void onCreate() {
        llBottom = findViewById(R.id.llBottom);
        tvTime = findViewById(R.id.tvTime);
        llType1 = findViewById(R.id.llType1);
        llType2 = findViewById(R.id.llType2);
        llType3 = findViewById(R.id.llType3);
        tvType0 = findViewById(R.id.tvType0);
        tvType1 = findViewById(R.id.tvType1);
        tvType2 = findViewById(R.id.tvType2);
        tvType3 = findViewById(R.id.tvType3);
        tvType4 = findViewById(R.id.tvType4);
        tvType5 = findViewById(R.id.tvType5);
        tvType6 = findViewById(R.id.tvType6);
        tvType7 = findViewById(R.id.tvType7);
        tvType8 = findViewById(R.id.tvType8);
        tvReset = findViewById(R.id.tvReset);
        tvSure = findViewById(R.id.tvSure);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) llBottom.getLayoutParams();
        params.setMargins(0, 0, 0, Utils.getStatusBarHeight(getContext()));
        llBottom.setLayoutParams(params);

//        1-正常 2-迟到 3-旷工 4-早退 5-补卡
        tvType0.setOnClickListener(v -> {
            setType(0);
        });
        tvType1.setOnClickListener(v -> {
            setType(1);
        });
        tvType2.setOnClickListener(v -> {
            setType(2);
        });
        tvType3.setOnClickListener(v -> {
            setType(3);
        });
        tvType4.setOnClickListener(v -> {
            setType(4);
        });
        tvType5.setOnClickListener(v -> {
            setType(5);
        });
        tvType6.setOnClickListener(v -> {
            setType(6);
        });
        tvType7.setOnClickListener(v -> {
            setType(7);
        });
        tvType8.setOnClickListener(v -> {
            setType(8);
        });
        tvTime.setOnClickListener(v -> {
            if (listener != null) {
                listener.onListItemClick(0, "");
            }
        });
        tvSure.setOnClickListener(v -> {
            if (listener != null) {
                listener.onListItemClick(1, "");
            }
        });
        tvReset.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                tvTime.setText("");
                setType(-1);
                if (listener != null) {
                    listener.onListItemClick(2, "");
                }
            }
        });

    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    public void setData(int type, String content) {
        if (type == 0) {
            tvTime.setText(content);
        }
    }

    public void setType(int type) {
        if (type <= 0) {
            if(type==-1){
                tvType0.setSelected(false);
            }else{
                tvType0.setSelected(true);
            }
            tvType1.setSelected(false);
            tvType2.setSelected(false);
            tvType3.setSelected(false);
            tvType4.setSelected(false);
            tvType5.setSelected(false);
            tvType6.setSelected(false);
            tvType7.setSelected(false);
            tvType8.setSelected(false);
        } else {
            tvType0.setSelected(false);
            if (type > 2) {
                if(type>5){
                    if (llType3.getChildAt(type - 6).isSelected()) {
                        llType3.getChildAt(type - 6).setSelected(false);
                    } else {
                        llType3.getChildAt(type - 6).setSelected(true);
                    }
                }else{
                    if (llType2.getChildAt(type - 3).isSelected()) {
                        llType2.getChildAt(type - 3).setSelected(false);
                    } else {
                        llType2.getChildAt(type - 3).setSelected(true);
                    }
                }
            } else {
                if (llType1.getChildAt(type).isSelected()) {
                    llType1.getChildAt(type ).setSelected(false);
                } else {
                    llType1.getChildAt(type ).setSelected(true);
                }
            }
        }
        changeTypeData();
    }
    public void changeTypeData() {
        // 设置 Adapter
        String content = "";
        if (!tvType0.isSelected()) {
            for (int i = 0; i < 2; i++) {
                if (llType1.getChildAt(i + 1).isSelected()) {
                    if (Utils.isNull(content)) {
                        content = (i + 1) + "";
                    } else {
                        content = content + "," + (i + 1);
                    }
                }

            }
            for (int i = 0; i < 3; i++) {
                if (llType2.getChildAt(i).isSelected()) {
                    if (Utils.isNull(content)) {
                        content = (i + 3) + "";
                    } else {
                        content = content + "," + (i + 3);
                    }
                }

            }
            for (int i = 0; i < 3; i++) {
                if (llType3.getChildAt(i).isSelected()) {
                    if (Utils.isNull(content)) {
                        content = (i + 6) + "";
                    } else {
                        content = content + "," + (i + 6);
                    }
                }

            }
        }
        if (listener != null) {
            listener.onListItemClick(3, content);
        }
    }
}


